Open
Conversation
reneesu99
commented
Oct 25, 2022
added get method to planets
refactored and added query param moons
…ate app method, created tests folder and checked GET /planets returns200 and test run and passes
added testing for complete code coverage
CheezItMan
reviewed
Nov 20, 2022
CheezItMan
left a comment
There was a problem hiding this comment.
Nice work Renee & Semhar, you hit the learning goals here. Well done!
Comment on lines
+11
to
+26
| def to_dict(self): | ||
| return { | ||
| "name": self.name, | ||
| "description": self.description, | ||
| "moons": self.moons, | ||
| "id": self.id, | ||
| } | ||
|
|
||
| @classmethod | ||
| def from_dict(cls, planet_data): | ||
| planet_1 = Planet( | ||
| name=planet_data["name"], | ||
| description=planet_data["description"], | ||
| moons=planet_data["moons"] | ||
| ) | ||
| return planet_1 No newline at end of file |
| @@ -0,0 +1,50 @@ | |||
| # from flask import jsonify, abort, make_response | |||
| @planet_bp.route("", methods=["POST", "GET"]) | ||
| def handle_planets(): | ||
| if request.method == "POST": | ||
| request_body = request.get_json() |
There was a problem hiding this comment.
Doing some validation here on the request body would be appropriate (make sure it has the required fields).
| planets = Planet.query.filter_by(moons = moons_param) | ||
| else: | ||
| planets= Planet.query.all() | ||
| response_body = [planet.to_dict() for planet in planets] |
There was a problem hiding this comment.
I like the list comprehension here and use of the to_dict method.
Comment on lines
+27
to
+37
| def validate_id(id, cls): | ||
| try: | ||
| id = int(id) | ||
| except: | ||
| abort(make_response ({"Message": f"{cls.__name__} {id} invalid."}, 400)) | ||
|
|
||
| # to access a planet with planet_id in our db, we use | ||
| obj = cls.query.get(id) | ||
| if not obj: | ||
| abort(make_response({"Message": f"{cls.__name__} {id} not found."}, 404)) | ||
| return obj |
| return planet.to_dict() | ||
|
|
||
| elif request.method == "PUT": | ||
| request_body = request.get_json() |
There was a problem hiding this comment.
Validating the request body would be good here.
| "description": "i luv storms" | ||
| } | ||
|
|
||
| def test_get_query_param_planets(client, two_saved_planets): |
|
|
||
|
|
||
|
|
||
| def test_post_planet(client): |
There was a problem hiding this comment.
Also testing a post request with an invalid request body would be great.
| # Assert | ||
| assert response.status_code == 201 | ||
|
|
||
| def test_update_planet(client, two_saved_planets): |
There was a problem hiding this comment.
Also testing a put request with an invalid request body would be great.
You should also test a put request with an invalid/missing planet id.
Comment on lines
+92
to
+100
| def test_delete_planet(client, two_saved_planets): | ||
| # Act | ||
| response = client.delete("/planets/1") | ||
| assert response.status_code == 200 | ||
|
|
||
| def test_delete_planet_invalid_id(client, two_saved_planets): | ||
| # Act | ||
| response = client.delete("/planets/random") | ||
| assert response.status_code == 400 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.